﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using SchoolLibrary;
using SchoolLibrary.Entity;
using SchoolLibrary.Entity.Person;
using SchoolLibrary.DataBase.DAO;

namespace SchoolLibrary.DataBase.Builder
{
    class BookBuilder : AbstractEntityBuilder
    {

        public override Identity Build(DataRow row)
        {
            Book book = new Book();
            BuildEntity(book, row);
            return book;
        }

        protected override void BuildEntity(Identity entity, DataRow row)
        {
            base.BuildEntity(entity, row);
            Book book = (Book)entity;
            book.Name = row[Book.NAME_COLUMN_NAME].ToString();
            book.Year = Int32.Parse(row[Book.YEAR_COLUMN_NAME].ToString());
            book.Price = Int32.Parse(row[Book.PRICE_COLUMN_NAME].ToString());
            book.Amount = Int32.Parse(row[Book.AMOUNT_COLUMN_NAME].ToString());
            List<Writer> writers = WriterDAO.getInstance().findWritersByBookId(book.Id);
            long genreId = Int64.Parse(row[Book.GENRE_ID_COLUMN_NAME].ToString());
            book.Genre = GenreDAO.getInstance().findById(genreId);
            book.Writers = writers;
        }
    }
}
